// 价格千分位分割

// 方法一，带符号￥
// function formatPrice(price) {
//   return price.toLocaleString("zh-CN", { style: "currency", currency: "CNY" });
// }

// 方法二 不带￥
// function formatPrice(number, locale = "zh-CN") {
//   const formatter = new Intl.NumberFormat(locale, {
//     style: "decimal",
//     minimumFractionDigits: 2,
//     maximumFractionDigits: 2,
//   });
//   return formatter.format(number);
// }

// function formatPrice(num) {
//   console.log(num);
//   var c =
//     String(num).indexOf(".") !== -1
//       ? num.toLocaleString()
//       : String(num).replace(/(\d)(?=(?:\d{2})+$)/g, "$1,");

//   console.log(c);
//   return c;
// }

//格式化钱数，三位加一逗号

function formatPrice(number) {
  number = number + "";

  number = number.replace(/\,/g, "");

  if (isNaN(number) || number == "") return "";

  number = Math.round(number * 100) / 100;

  if (number < 0)
    return (
      "-" +
      outputdollars(Math.floor(Math.abs(number) - 0) + "") +
      outputcents(Math.abs(number) - 0)
    );
  else
    return outputdollars(Math.floor(number - 0) + "") + outputcents(number - 0);
}

//格式化金额

function outputdollars(number) {
  if (number.length <= 3) return number == "" ? "0" : number;
  else {
    var mod = number.length % 3;

    var output = mod == 0 ? "" : number.substring(0, mod);

    for (let i = 0; i < Math.floor(number.length / 3); i++) {
      if (mod == 0 && i == 0) {
        output += number.substring(mod + 3 * i, mod + 3 * i + 3);
      } else {
        output += "," + number.substring(mod + 3 * i, mod + 3 * i + 3);
      }
    }

    return output;
  }
}

function outputcents(amount) {
  amount = Math.round((amount - Math.floor(amount)) * 100);

  return amount < 10 ? ".0" + amount : "." + amount;
}

export { formatPrice };
